// pages/channel/divideManage/divideManage.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_id: '', // 渠道用户id
    channel_promoter_id: '', // 渠道推广id
    divideArr: [  // 选择的产品数组
      {
        equipments_id:'',
        equipments_name: '',
        money: '1'
      }
    ], 
    detail: [], // 获取的分成详情
    search: '', //产品搜索
    result_index: -1, //显示搜索产品的行数索引
    goodsList: [], // 搜索到的产品列表
    is_old: false, //是否显示原来的数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      user_id: options.user_id,
      channel_promoter_id: options.channel_promoter_id
    })
    this.getInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  // 获取分成详情
  getInfo(){
    var that = this;
    app.ajax({
      url: 'User/Channel/getPromotersCommissionList',
      data: {
        channel_promoter_id: that.data.channel_promoter_id
      },
      success(res) {
        if (res.data.code == 1000) {
          that.setData({
            divideArr: res.data.data,
            detail: res.data.data
          })
        } else {
          that.setData({
            divideArr: [ 
              {
                equipments_id:'',
                equipments_name: '',
                money: '1'
              }
            ], 
            detail: []
          })
        }
      }
    })
  },
  // 提交
  submit(){
    var that = this;
    var user_id = wx.getStorageSync('user_id');
    app.ajax({
      url: 'User/Channel/setPromoteCommission',
      data: {
        user_id: user_id,
        channel_promoter_id: that.data.channel_promoter_id,
        value: that.data.divideArr
      },
      success(res) {
        if (res.data.code == 1000) {
          app.showToast('添加成功', "none", 2000, function () { });
          wx.navigateBack();
        }else{
          app.showToast(res.data.message, "none", 2000, function () { });
        }
      }
    })
  },

  /* ------------- 产品S ---------------*/
  // 获取产品列表
  getList() {
    var that = this;
    let search = that.data.search;
    app.ajax({
      url: 'User/Equipment/getEquipmentsList',
      data: {
        key: search
      },
      success(res) {
        if (res.data.code == 1000) {
          that.setData({
            goodsList: res.data.data.data
          })
        }
      }
    })
  },
  // 点击产品输入框
  clickDivide(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let {
      divideArr,
    } = that.data;
    divideArr[index].equipments_id = '';
    divideArr[index].equipments_name = '';
    that.setData({
      result_index: index,
      divideArr: divideArr,
      search: '',
      is_old: false,
      is_select: false
    })
  },

  // 搜索产品
  divideValue(e) {
    let that = this;
    that.setData({
      result_index: e.currentTarget.dataset.index,
      search: e.detail.value
    })
    that.getList();
  },
  divideMoney(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let divideArr = that.data.divideArr;
    divideArr[index].money = e.detail.value;
    that.setData({
      divideArr: divideArr,
    })
  },
  
  // 隐藏
  hideResult() {
    let that = this;
    let {
      result_index,
      keyboard,
      divideArr,
      search
    } = that.data;
    if (result_index == -1) {
      return;
    }
    that.checkIsSelect(result_index);
  },
  // 选择产品
  selectGoods(e) {
    let that = this;
    let {
      goodsList,
      divideArr
    } = that.data;
    let idx = e.currentTarget.dataset.idx;
    let index = e.currentTarget.dataset.index;
    divideArr[index].equipments_id = goodsList[idx].equipments_id;
    divideArr[index].equipments_name = goodsList[idx].equipments_name;
    divideArr[index].money = 1;
    that.setData({
      divideArr: divideArr,
    })
    that.checkIsSelect(index);
  },
  // 检测是否选择产品
  checkIsSelect(index) {
    let that = this;
    let {
      detail,
      divideArr,
      search,
      goodsList
    } = that.data;
    let isSelect = divideArr[index].equipments_id != '' ? true : false;
    if (isSelect) {
      that.setData({
        result_index: -1,
        goodsList: [],
        search: '',
        is_select: true
      })
    } else {
      if (search == '' || goodsList.length == 0) {
        let is_old = false;
        detail.forEach((item) => {
          if (item.equipments_id == divideArr[index].equipments_id) {
            divideArr[index].equipments_id = item.equipments_id;
            divideArr[index].equipments_name = item.equipments_name;
            divideArr[index].money = item.money;
            is_old = true;
          }
        })
        if (!is_old) {
          divideArr[index].equipments_id = '';
          divideArr[index].equipments_name = '';
        }
        that.setData({
          divideArr: divideArr,
          search: '',
          result_index: -1,
          is_old: is_old
        })
      }
    }

  },
  // 输入产品金额
  clickNum(e) {
    let that = this;
    let value = e.detail.value;
    let index = e.currentTarget.dataset.index;
    let divideArr = that.data.divideArr;
    divideArr[index].money = value;
    console.log(divideArr, "divideArr", value,e)
    that.setData({
      divideArr: divideArr
    })
  },
  // 添加产品
  addDivide(){
    let that = this;
    let divideArr = that.data.divideArr;
    divideArr.push({
      equipments_id:'',
      equipments_name: '',
      money: ''
    })
    that.setData({
      divideArr: divideArr,
      result_index: -1,
      search: ''
    })
  },
  // 删除产品
  reduceDivide(e){
    let that = this;
    let index = e.currentTarget.dataset.index;
    let divideArr = that.data.divideArr;
    divideArr.splice(index, 1);
    that.setData({
      divideArr: divideArr
    })
  },
})